{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f04dd603-a2d1-48ce-8c17-9f1dba8de1ee",
   "metadata": {},
   "source": [
    "Chapter 05\n",
    "\n",
    "# SymPy计算逆矩阵\n",
    "《线性代数》 | 鸢尾花书：数学不难"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ccafb456-2453-4c82-8a65-b1963a370cb2",
   "metadata": {},
   "source": [
    "这段代码的目标是计算矩阵 $ K $ 的逆矩阵 $ K^{-1} $，并通过矩阵乘法验证逆矩阵的正确性。计算矩阵的逆在数学上是一个重要的操作，通常用于解线性方程组、坐标变换和优化问题。\n",
    "\n",
    "### 1. 初始化并定义矩阵\n",
    "\n",
    "代码首先导入 `sympy` 库，它是 Python 的符号计算库，可以用于精确的代数计算。然后，代码定义了一个 $2 \\times 2$ 矩阵：\n",
    "\n",
    "$$\n",
    "K = \\begin{bmatrix} 1 & 0.5 \\\\ 0 & 1 \\end{bmatrix}\n",
    "$$\n",
    "\n",
    "这里使用 `sp.Matrix([...])` 语法来创建 `sympy` 矩阵，它与 `numpy` 数组类似，但支持符号计算，不会自动转换为浮点数。\n",
    "\n",
    "### 2. 计算矩阵的逆\n",
    "\n",
    "计算矩阵的逆 $ K^{-1} $ 需要先判断 $ K $ 是否可逆。一个 $ 2 \\times 2 $ 矩阵 \n",
    "\n",
    "$$\n",
    "A = \\begin{bmatrix} a & b \\\\ c & d \\end{bmatrix}\n",
    "$$\n",
    "\n",
    "的逆矩阵公式为：\n",
    "\n",
    "$$\n",
    "A^{-1} = \\frac{1}{\\det(A)} \\begin{bmatrix} d & -b \\\\ -c & a \\end{bmatrix}\n",
    "$$\n",
    "\n",
    "其中行列式（determinant）为：\n",
    "\n",
    "$$\n",
    "\\det(A) = ad - bc\n",
    "$$\n",
    "\n",
    "对于矩阵 $ K $，行列式计算如下：\n",
    "\n",
    "$$\n",
    "\\det(K) = (1 \\times 1) - (0.5 \\times 0) = 1\n",
    "$$\n",
    "\n",
    "因为 $\\det(K) \\neq 0$，矩阵 $ K $ 可逆，其逆矩阵计算如下：\n",
    "\n",
    "$$\n",
    "K^{-1} = \\frac{1}{1} \\begin{bmatrix} 1 & -0.5 \\\\ 0 & 1 \\end{bmatrix}\n",
    "= \\begin{bmatrix} 1 & -0.5 \\\\ 0 & 1 \\end{bmatrix}\n",
    "$$\n",
    "\n",
    "代码中的 `K_inv = K.inv()` 直接调用 `sympy` 的 `.inv()` 方法，它自动检查矩阵是否可逆，并计算其逆矩阵。\n",
    "\n",
    "### 3. 验证逆矩阵的正确性\n",
    "\n",
    "代码使用 `K @ K_inv` 计算矩阵 $ K $ 与其逆矩阵 $ K^{-1} $ 的乘积。矩阵乘法定义为：\n",
    "\n",
    "$$\n",
    "K K^{-1} = \\begin{bmatrix} 1 & 0.5 \\\\ 0 & 1 \\end{bmatrix} \n",
    "\\begin{bmatrix} 1 & -0.5 \\\\ 0 & 1 \\end{bmatrix}\n",
    "$$\n",
    "\n",
    "通过矩阵乘法计算：\n",
    "\n",
    "$$\n",
    "\\begin{bmatrix} (1 \\times 1 + 0.5 \\times 0) & (1 \\times -0.5 + 0.5 \\times 1) \\\\ (0 \\times 1 + 1 \\times 0) & (0 \\times -0.5 + 1 \\times 1) \\end{bmatrix}\n",
    "=\n",
    "\\begin{bmatrix} 1 & 0 \\\\ 0 & 1 \\end{bmatrix}\n",
    "$$\n",
    "\n",
    "即单位矩阵 $ I $，证明 $ K^{-1} $ 确实是 $ K $ 的逆。\n",
    "\n",
    "同样，计算 $ K^{-1} K $：\n",
    "\n",
    "$$\n",
    "K^{-1} K = \\begin{bmatrix} 1 & -0.5 \\\\ 0 & 1 \\end{bmatrix} \n",
    "\\begin{bmatrix} 1 & 0.5 \\\\ 0 & 1 \\end{bmatrix}\n",
    "$$\n",
    "\n",
    "计算得到：\n",
    "\n",
    "$$\n",
    "\\begin{bmatrix} (1 \\times 1 + (-0.5) \\times 0) & (1 \\times 0.5 + (-0.5) \\times 1) \\\\ (0 \\times 1 + 1 \\times 0) & (0 \\times 0.5 + 1 \\times 1) \\end{bmatrix}\n",
    "=\n",
    "\\begin{bmatrix} 1 & 0 \\\\ 0 & 1 \\end{bmatrix}\n",
    "$$\n",
    "\n",
    "这再次证明 $ K^{-1} $ 是 $ K $ 的正确逆矩阵。\n",
    "\n",
    "### 4. 代码的数学意义\n",
    "\n",
    "这段代码使用 `sympy` 符号计算库，确保计算的准确性，并提供了逆矩阵的验证方法。在数学上，若矩阵 $ K $ 可逆，则满足：\n",
    "\n",
    "$$\n",
    "K K^{-1} = K^{-1} K = I\n",
    "$$\n",
    "\n",
    "其中 $ I $ 是单位矩阵。代码不仅计算了 $ K^{-1} $，还通过矩阵乘法验证了其正确性。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9f33ad30-4825-4db5-a990-29125da82028",
   "metadata": {},
   "source": [
    "## 初始化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "954b7525-dcef-47cb-af13-40c741ca3891",
   "metadata": {},
   "outputs": [],
   "source": [
    "import sympy as sp"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8c1352a6-38be-426c-b177-a0ea0aba4ba8",
   "metadata": {},
   "source": [
    "## 定义矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "94564c8c-7b83-46be-9ab5-0ebf5ed5b3b6",
   "metadata": {},
   "outputs": [],
   "source": [
    "K = sp.Matrix([[1, 0.5],\n",
    "               [0, 1]])\n",
    "K"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "1478ce88-e015-4cfb-8d58-53eef4ddb12a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}1.0 & -0.5\\\\0 & 1.0\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[1.0, -0.5],\n",
       "[  0,  1.0]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "K_inv = K.inv()\n",
    "K_inv"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "94467e35-a18d-4c20-bf7a-6767f4f111ca",
   "metadata": {},
   "source": [
    "## 检验"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "6b1d2c94-c81b-4d5d-8ee1-339eea7a218c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}1.0 & 0\\\\0 & 1.0\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[1.0,   0],\n",
       "[  0, 1.0]])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "K @ K_inv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "4db367b1-38dc-493c-b012-b09da4362536",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}1.0 & 0\\\\0 & 1.0\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[1.0,   0],\n",
       "[  0, 1.0]])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "K_inv @ K"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "070c3389-8048-43a3-baa7-6666009bce96",
   "metadata": {},
   "source": [
    "作者\t**生姜DrGinger**  \n",
    "脚本\t**生姜DrGinger**  \n",
    "视频\t**崔崔CuiCui**  \n",
    "开源资源\t[**GitHub**](https://github.com/Visualize-ML)  \n",
    "平台\t[**油管**](https://www.youtube.com/@DrGinger_Jiang)\t\t\n",
    "\t\t[**iris小课堂**](https://space.bilibili.com/3546865719052873)\t\t\n",
    "\t\t[**生姜DrGinger**](https://space.bilibili.com/513194466)  "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:base] *",
   "language": "python",
   "name": "conda-base-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
